package com.yupi.yuoj.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.service.UserService;
import com.yupi.yuoj.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class UserCleanJob {

    @Resource
    private UserMapper userMapper;

    /**
     * 每日 3 时执行一次，清理已删除超过一个月的用户
     */
    @Scheduled(cron = "0 0 3 * * *")
    public void cleanDeletedUsers() {
        log.info("开始执行已删除用户清理任务");

        // 计算一个月前的时间
        long oneMonthAgo = System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000;
        Date oneMonthAgoDate = new Date(oneMonthAgo);

        // 使用原生 SQL 执行物理删除
        int result = userMapper.deleteSqlRaw(oneMonthAgoDate);
        
        log.info("已删除用户清理任务执行完成，成功删除数量：{}", result);
    }
} 